home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Buttons and Labels and Scrolls (Oh, My!) / CustomCheckBox / CustomCheckBox.cs next >
Encoding:
Text File  |  2001-01-15  |  2.0 KB  |  63 lines

  1. //---------------------------------------------
  2. // CustomCheckBox.cs ⌐ 2001 by Charles Petzold
  3. //---------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class CustomCheckBox: Form
  9. {
  10.      public static void Main()
  11.      {
  12.           Application.Run(new CustomCheckBox());
  13.      }
  14.      public CustomCheckBox()
  15.      {
  16.           Text = "Custom CheckBox Demo";
  17.  
  18.           int      cyText = Font.Height;
  19.           int      cxText = cyText / 2;
  20.           FontStyle[] afs = { FontStyle.Bold,      FontStyle.Italic, 
  21.                               FontStyle.Underline, FontStyle.Strikeout };
  22.  
  23.           Label label    = new Label();
  24.           label.Parent   = this;
  25.           label.Text     = Text + ": Sample Text";
  26.           label.AutoSize = true;
  27.  
  28.           for (int i = 0; i < 4; i++)
  29.           {
  30.                FontStyleCheckBox chkbox = new FontStyleCheckBox();
  31.                chkbox.Parent = this;
  32.                chkbox.Text = afs[i].ToString();
  33.                chkbox.fontstyle = afs[i];
  34.                chkbox.Location = new Point(2 * cxText, 
  35.                                                (4 + 3 * i) * cyText / 2);
  36.                chkbox.Size = new Size(12 * cxText, cyText);
  37.                chkbox.CheckedChanged += 
  38.                               new EventHandler(CheckBoxOnCheckedChanged);
  39.           }
  40.      }
  41.      void CheckBoxOnCheckedChanged(object obj, EventArgs ea)
  42.      {
  43.           FontStyle fs = 0;
  44.           Label     label = null;
  45.  
  46.           for (int i = 0; i < Controls.Count; i++)
  47.           {
  48.                Control ctrl = Controls[i];
  49.  
  50.                if (ctrl.GetType() == typeof(Label))
  51.                     label = (Label) ctrl;
  52.  
  53.                else if (ctrl.GetType() == typeof(FontStyleCheckBox))
  54.                     if (((FontStyleCheckBox) ctrl).Checked)
  55.                          fs |= ((FontStyleCheckBox) ctrl).fontstyle;
  56.           }
  57.           label.Font = new Font(label.Font, fs);
  58.      }
  59. }
  60. class FontStyleCheckBox: CheckBox
  61. {
  62.      public FontStyle fontstyle;
  63. }